// /router/index.js

import route from './route'
import { LOGINPATCH } from '@/constants/router/subPackages'
import { ME } from '@/constants/router/tabBar'

// 配置白名单
const whiteList = [LOGINPATCH, ME]

const handleOverwirteRoute = () => {
  // 重写switchTab、navigateBack
  const methodToPatch = ['switchTab', 'navigateBack']
  methodToPatch.map((type) => {
    // 通过遍历的方式分别取出，uni.switchTab、uni.navigateBack
    // 并且对相应的方法做重写
    const original = uni[type]
    uni[type] = function (options = {}) {
      const { url: path } = options
      if (!whiteList.includes(path) && !uni.getStorageSync('AccessToken')) {
        // 判断是否存在token，不存在重定向到登录页
        uni.$e.route(LOGINPATCH)
      } else if (uni.getStorageSync('AccessToken') && path === LOGINPATCH) {
        // 判断是否存在token，存在则跳转到首页
        uni.$e.route(ME)
      } else {
        return original.call(this, options)
      }
    }
  })
}

const install = function (Vue, options) {
  uni.$e = { route }
  Vue.prototype.route = route
  // 重写uni方法
  handleOverwirteRoute()
  // 路由拦截器
  uni.$e.routeIntercept = (routeConfig, resolve) => {
    const path = routeConfig.url.split('?')[0]
    if (!whiteList.includes(path) && !uni.getStorageSync('AccessToken')) {
      uni.$e.route(LOGINPATCH)
      return
    }
    resolve(true)
  }
}
export default {
  install
}
